/**
 * This class represents the Ask Western module in the iWestern application
 * @author Chirag Mistry
 */

package ca.uwo.android.askwestern;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import ca.uwo.android.BaseMenuActivity;
import ca.uwo.android.R;

public class AskWestern extends BaseMenuActivity {

	/**
	 * This method is called when the Ask Western module is first loaded
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// set the layout
		setContentView(R.layout.aw_main); 

		// get the list view from the UI
		ListView categoryList = (ListView) findViewById(R.id.categories);
		// get the string array of question categories
		final String[] stringArrayCat = getResources().getStringArray(
				R.array.categories_array);
		// create an array adapter that will be used to populate the
		// categoryList list view
		final ArrayAdapter<String> holdCat = new ArrayAdapter<String>(this,
				R.layout.aw_optionlist_item, stringArrayCat);

		// populate list by setting the adapter
		categoryList.setAdapter(holdCat); 

		categoryList.setOnItemClickListener(new OnItemClickListener() {

			/**
			 * This method is called when a category in the list is clicked;
			 * based on which category is selected, the user will be redirected
			 * to the appropriate website. A new activity will be started to
			 * display the webpage in the application itself rather than a
			 * browser.
			 * 
			 * @param view
			 *            the view that was clicked (i.e. a text view in this
			 *            case)
			 * @param position
			 *            the index of the item that was clicked
			 */
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				// url to redirect to
				String url = ""; 
				
				// get text of clicked item
				String itemClicked = ((TextView) view).getText().toString(); 
				
				// get the appropriate URL to launch based on category clicked
				if (itemClicked == stringArrayCat[0]) {
					url = "http://uwo.intelliresponse.com/m/ugrad/";
				} else if (itemClicked == stringArrayCat[1]) {
					url = "http://uwo.intelliresponse.com/m/faculty/";
				} else if (itemClicked == stringArrayCat[2]) {
					url = "http://uwo.intelliresponse.com/m/grad/";
				} else if (itemClicked == stringArrayCat[3]) {
					url = "http://uwo.intelliresponse.com/m/its/";
				} else if (itemClicked == stringArrayCat[4]) {
					url = "http://uwo.intelliresponse.com/m/lib/";
				} else if (itemClicked == stringArrayCat[5]) {
					url = "http://uwo.intelliresponse.com/m/alumni/";
				} else if (itemClicked == stringArrayCat[6]) {
					url = "http://uwo.intelliresponse.com/m/archives/";
				}

				// start another activity to display the url
				Intent openWebpageInApp = new Intent(AskWestern.this,
						ca.uwo.android.ViewWebPage.class);
				openWebpageInApp.putExtra("URL", url);
				startActivity(openWebpageInApp);
			}
		});
	}
}